package cn.lds.j375.chlm.run;

import cn.lds.j375.chlm.dto.ResultDTO;
import cn.lds.j375.chlm.entity.Menu;
import cn.lds.j375.chlm.entity.Order;
import cn.lds.j375.chlm.entity.OrderDetails;
import cn.lds.j375.chlm.entity.User;
import cn.lds.j375.chlm.service.MenuService;
import cn.lds.j375.chlm.service.OrderService;
import cn.lds.j375.chlm.service.UserService;
import cn.lds.j375.chlm.service.impl.MenuServiceImpl;
import cn.lds.j375.chlm.service.impl.OrderServiceImpl;
import cn.lds.j375.chlm.service.impl.UserServiceImpl;

import java.awt.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public class Chlm {
    public static void main(String[] args) throws Exception {

        UserService userService = new UserServiceImpl();
        MenuService menuService = new MenuServiceImpl();
        OrderService orderService = new OrderServiceImpl();

        Scanner input = new Scanner(System.in);

        //创建User对象，保存登录用户
        User loginUser = null;

        boolean isQuit = true;

        while (isQuit){
            System.out.println("***********************************");
            System.out.println("***********************************");
            System.out.println("*********欢迎光临大帅美食**************");
            System.out.println("***********************************");
            System.out.println("***********************************");

            System.out.println("请问能帮你做些什么");
            System.out.println("1.查看菜品");
            System.out.println("2.我要点菜");
            System.out.println("3.用户注册");
            System.out.println("4.用户登录");
            System.out.println("5.我的订单");
            System.out.println("6.再见");

            int choose = input.nextInt();

            switch (choose){
                case 1 -> {
                    ResultDTO<List<Menu>> resultDTO = menuService.selectMenuAll();
                    resultDTO.getData().forEach(System.out::println);

                }
                case 2 -> {
                    if (Objects.isNull(loginUser)){
                        System.out.println("请登录后再来下单");
                        break;
                    }

                    ResultDTO<List<Menu>> resultDTO = menuService.selectMenuAll();


                    List<OrderDetails> orderDetails = new ArrayList<>();

                    String isOk = "y";

                    while ("y".equals(isOk)){
                        resultDTO.getData().forEach(System.out::println);
                        System.out.println("请选择菜品编号");
                        OrderDetails od = new OrderDetails();
                        od.setMenuId(input.nextInt());
                        System.out.println("请选择菜品的数量");
                        od.setCount(input.nextInt());
                        orderDetails.add(od);
                        System.out.println("还需要继续点菜吗（y/n）");
                        isOk = input.next();
                    }

                    //我们需要把List修改为数组
                    OrderDetails[] ods = new OrderDetails[orderDetails.size()];

                    for (int i = 0; i < orderDetails.size(); i++) {
                        ods[i] = orderDetails.get(i);
                    }

                    //提交订单
                    ResultDTO result = orderService.addOrder(loginUser.getId(), ods);

                    System.out.println(result);

                }
                case 3->{
                    User regUser = new User();
                    System.out.println("请输入您的姓名");
                    regUser.setName(input.next());
                    System.out.println("请输入您的用户名");
                    regUser.setUsername(input.next());
                    System.out.println("请输入您的密码");
                    regUser.setPassword(input.next());
                    System.out.println("请输入您的手机号");
                    regUser.setMobileNo(input.next());
                    System.out.println("请输入您的地址");
                    regUser.setAddress(input.next());
                    System.out.println("您的充值金额");
                    regUser.setMoney(new BigDecimal(input.next()));

                    ResultDTO resultDTO = userService.regUser(regUser);
                    System.out.println(resultDTO.getMsg());
                }
                case 4->{
                    User user = new User();
                    System.out.println("请输入用户名");
                    user.setUsername(input.next());
                    System.out.println("请输入密码");
                    user.setPassword(input.next());
                    ResultDTO<User> userResultDTO = userService.loginUser(user);
                    loginUser = userResultDTO.getData();
                    System.out.println(userResultDTO.getMsg());

                }
                case 5->{
                    if (Objects.isNull(loginUser)){
                        System.out.println("没有登录，请登录后再来");
                        break;
                    }

                    ResultDTO<List<Order>> listResultDTO = orderService.selectOrderByUserId(loginUser.getId());

                    List<Order> orders = listResultDTO.getData();

                    if (Objects.nonNull(orders) || orders.size() !=0){
                        orders.forEach(order -> {
                            System.out.println(order.getId() + "," + order.getUser().getName() + "," + order.getUser().getMobileNo() + " , " + order.getUser().getAddress() + "," + order.getTotalPrice());

                            List<OrderDetails> orderDetailes = order.getOrderDetailes();
                            orderDetailes.forEach(od -> {
                                System.out.println(od.getId() +","+ od.getMenu().getMenuName() +"," +  od.getCount() +"," + od.getOrderDetailPrice());
                            });
                        });
                    }
                }
                case 6->{
                    isQuit = false;
                }
            }

            System.out.println("欢迎您再次光临");

        }




    }
}
